package codetop.greedy;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

public class Code1353 {
    public int maxEvents(int[][] events) {
        if (events.length<=1)return events.length;
        Arrays.sort(events, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if (o1[0]==o2[0])return o1[1]-o2[1];
                return o1[0]-o2[0];
            }
        });
        PriorityQueue<Integer>queue=new PriorityQueue<>();
        int day=1;
        int i=0;
        int ans=0;
        while (i<events.length||!queue.isEmpty()){
            while (i<events.length&&events[i][0]<=day){
                queue.offer(events[i][1]);
                i++;
            }
            while (!queue.isEmpty()&&queue.peek()<day) {
                queue.remove();
            }
            if (!queue.isEmpty()){
                queue.poll();
                ans++;
            }
            day++;
        }
        return ans;
    }
}
